import { ELType, GroupType } from "./parse/type/ELType"
import { NodeConfig } from '@logicflow/core';




export interface FormConfig {
    prop: string
    label: string
    type: string,
    allowMul?: boolean, // 选择框是否允许多选
    allowCreate?: boolean,// 选择框是否允许自定义标签
    options?: {
        key: string,
        value: string | boolean
    }[],
    show?: (data: any) => boolean
}
const defaultNodeConfig: FormConfig[] = [
    {
        prop: "nodeId",
        label: "节点ID",
        type: "input"
    },
    {
        prop: "name",
        label: "节点名称",
        type: "input"
    }, {
        prop: "tag",
        label: "节点别名",
        type: "input"
    }, {
        prop: "data",
        label: "节点数据",
        type: "textarea"
    }
]

export const parseConfig: Record<ELType, FormConfig[]> = {
    "ID": defaultNodeConfig,
    "THEN": [],
    "WHEN": [],
    "SWITCH": defaultNodeConfig,
    "IF": defaultNodeConfig,
    "GROUP": [
        {
            prop: "nodeId",
            label: "节点ID",
            type: "input"
        },
        {
            prop: "name",
            label: "节点名称",
            type: "input"
        },
        {
            prop: "groupType",
            label: "分组类型",
            type: "select",
            options: Object.keys(GroupType).map((v: string) => {
                return { key: v, value: v };
            })
        }, {
            prop: "startNum",
            label: "FOR迭代次数",
            type: "input",
            show: (data) => {
                return data.groupType === GroupType.FOR;
            }
        }, {
            prop: "aliasId",
            label: "选择编排中的id语法",
            type: "input",
            show: (data) => {
                return data.groupType === GroupType.CONFIG;
            }
        }, {
            prop: "tag",
            label: "选择编排中的tag语法",
            type: "input",
            show: (data) => {
                return data.groupType === GroupType.CONFIG;
            }
        }, {
            prop: "ignoreError",
            label: "并行：忽略错误",
            type: "select",
            options: [{ key: "TRUE", value: true }, { key: "FALSE", value: false }],
            show: (data) => {
                return data.groupType === GroupType.CONFIG;
            }
        }, {
            prop: "any",
            label: "并行：任一节点先执行完则忽略其他",
            type: "select",
            options: [{ key: "TRUE", value: true }, { key: "FALSE", value: false }],
            show: (data) => {
                return data.groupType === GroupType.CONFIG;
            }
        }, {
            prop: "must",
            label: "并行：指定任意节点先执行完则忽略其他",
            type: "select",
            allowCreate: true,
            allowMul: true,
            options: [],
            show: (data) => {
                return data.groupType === GroupType.CONFIG;
            }
        },
    ],
    [ELType.BREAK]: [],
    [ELType.OR]: [],
    [ELType.AND]: [],
    [ELType.NOT]: []
}

export const getEdgeForm = (sourceNode: NodeConfig): FormConfig[] => {
    let texts: string[] = []
    if (ELType.SWITCH === sourceNode.type) {
        return [{
            prop: "value",
            label: "id",
            type: "input"
        }]
    }


    if (ELType.IF === sourceNode.type) {
        texts = ["TRUE", "FALSE"]
    } else if (ELType.GROUP === sourceNode.type) {
        switch (sourceNode.properties?.groupType) {
            case GroupType.LOGIC:
                texts = ["TRUE", "FALSE"]
                break
            case GroupType.CATCH:
                texts = ["EXCEPTION"]
                break
            case GroupType.FOR:
            case GroupType.WHILE:
            case GroupType.ITERATOR:
                texts = ["START", "BREAK"]
                break
        }
    }


    return [{
        prop: "value",
        label: "标签",
        type: "select",
        options: texts.map((v: string) => {
            return { key: v, value: v };
        })
    }]
}